package com.xplay.xpocker.room.message;

import com.xplay.xpocker.constant.MessageNoticeEnum;
import com.xplay.xpocker.room.Message;
import com.xplay.xpocker.util.JacksonStringUtil;
import lombok.Data;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

/**
 * 用于消息构建
 */


@Data
public class MessageContentFactory {


    List<MessageContent<MessageToClient>> messageList;
    BigDecimal messageToUid;
    MessageNoticeEnum mnEnum;
    String roomCode;
    Integer type;
    MessageToClient body;

    MessageContentFactory operation;

    public MessageContentFactory() {
    }

    public MessageContentFactory(String roomCode, Integer type, BigDecimal messageToUid, MessageNoticeEnum mnEnum) {
        this.messageToUid = messageToUid;
        this.mnEnum = mnEnum;
        this.type = type;
        this.roomCode = roomCode;
        messageList = new ArrayList<>();
        body = new MessageToClient();
    }

    public MessageContentFactory multi(Object... objects) {
        body.multi(objects);
        return this;
    }

    public MessageContentFactory remove(String... keys) {
        for (String key : keys) {
            body.remove(key);
        }
        return this;
    }

    /**
     * 修改 操作对象
     *
     * @param objects
     * @return
     */

    public MessageContentFactory modify(Object... objects) {
        if (operation == null) {
            operation = this.clone();
        }
        operation.multi(objects);
        return this;
    }

    /**
     * 脱敏数据
     *
     * @param mnEnum
     * @param keys
     * @return
     */


    public MessageContentFactory filter(MessageNoticeEnum mnEnum, String keys) {
        if (operation == null) {
            operation = this.clone();
        }
        operation.mnEnum = mnEnum;
        operation.getBody().multiRemove(keys);
        return this;
    }

    public MessageContentFactory covert(MessageNoticeEnum mnEnum, String key, Supplier<Object> supplier) {
        if (operation == null) {
            operation = this.clone();
        }
        operation.mnEnum = mnEnum;
        operation.getBody().put(key, supplier.get());
        return this;
    }


    public MessageContentFactory send() {
        if (operation == null) {
            messageList.add(MessageContent.<MessageToClient>builder(this.roomCode, this.type, this.messageToUid, this.mnEnum).setBody(this.getBody()).build());
        } else {
            messageList.add(MessageContent.<MessageToClient>builder(operation.roomCode, operation.type, operation.messageToUid, operation.mnEnum).setBody(operation.getBody()).build());
        }
        operation = null;
        return this;
    }


    public static MessageContentFactory init(String roomCode, Integer type, BigDecimal messageToUid, MessageNoticeEnum mnEnum) {

        return new MessageContentFactory(roomCode, type, messageToUid, mnEnum);
    }

    public static void main(String[] args) {
        System.out.println(JacksonStringUtil.objectToString(MessageContentFactory.
                init("12345", 1, new BigDecimal("1"), MessageNoticeEnum.CURRENT)
                .multi("fff", "ccc", "eee", "ggg").send().filter(MessageNoticeEnum.OTHER, "eee").send()
                .multi("aaa", "ffff").getMessageList()));
    }

    @Override
    protected MessageContentFactory clone() {
        String factoryJson = JacksonStringUtil.objectToString(this);
        return JacksonStringUtil.objectCase(factoryJson, MessageContentFactory.class);
    }
}
